﻿using LS.Screening.UI.Common;
using ReactiveUI.Fody.Helpers;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace LS.DicomViewer.ViewModels
{
    public class ImageViewModel : ViewModel
    {
        #region 属性/变量
        
        /// <summary>
        /// 图片地址
        /// </summary>
        [Reactive]
        public string ImageFilePath { get; set; }

        /// <summary>
        /// 位图
        /// </summary>
        [Reactive]
        public BitmapSource ImageSource { get; set; }

        /// <summary>
        /// 是否显示操作工具栏
        /// </summary>
        [Reactive]
        public bool ShowDrawToolbar { get; set; } = true;

        #endregion

        #region 方法

        /// <summary>
        /// 从路径中加载影像文件
        /// </summary>
        /// <param name="path"></param>
        public async Task Load(string path)
        {
            ImageFilePath = path;
            await ShowImage();
        }

        public void Clear()
        {
            Dispatcher.Invoke(() => ImageSource = null);
        }

        private async Task ShowImage()
        {
            if (!File.Exists(ImageFilePath)) return;
            await Dispatcher.InvokeAsync(() =>
              {
                  ImageSource = new BitmapImage(new Uri(ImageFilePath, UriKind.Absolute));
              });
        }

        #endregion
    }
}
